﻿using Sidvall.UI.Dialogs;
using System.Threading.Tasks;

namespace Sidvall.UI.Wpf.Dialogs
{
	public class MessageBoxDialog : IMessageDialog
	{
		#region Public Members

		#region ShowMessageAsync

		public Task ShowMessageAsync(MessageContext message)
		{
			System.Windows.MessageBoxButton buttons;
			System.Windows.MessageBoxResult result;
			System.Windows.MessageBoxImage icon;

			if (message == null)
				return Task.FromResult(true);
			result = System.Windows.MessageBoxResult.None;
			if (message.MessageOptions.Contains(Sidvall.Resources.ResourceKeys.YesOptionId))
			{
				if (message.MessageOptions.Contains(Sidvall.Resources.ResourceKeys.CancelOptionId))
					buttons = System.Windows.MessageBoxButton.YesNoCancel;
				else
					buttons = System.Windows.MessageBoxButton.YesNo;
			}
			else if (message.MessageOptions.Contains(Sidvall.Resources.ResourceKeys.OkOptionId))
			{
				if (message.MessageOptions.Contains(Sidvall.Resources.ResourceKeys.CancelOptionId))
					buttons = System.Windows.MessageBoxButton.OKCancel;
				else
					buttons = System.Windows.MessageBoxButton.OK;
			}
			else
			{
				buttons = System.Windows.MessageBoxButton.OK;
			}
			switch (message.MessageType)
			{
				case MessageType.Question:
					icon = System.Windows.MessageBoxImage.Question;
					break;
				case MessageType.Warning:
					icon = System.Windows.MessageBoxImage.Warning;
					break;
				case MessageType.Error:
					icon = System.Windows.MessageBoxImage.Error;
					break;
				default:
					icon = System.Windows.MessageBoxImage.Information;
					break;
			}
			if (message.MessageOptions.DefaultMessageOption != null)
			{
				switch (message.MessageOptions.DefaultMessageOption.Id)
				{
					case Sidvall.Resources.ResourceKeys.CancelOptionId:
						result = System.Windows.MessageBoxResult.Cancel;
						break;
					case Sidvall.Resources.ResourceKeys.OkOptionId:
						result = System.Windows.MessageBoxResult.OK;
						break;
					case Sidvall.Resources.ResourceKeys.YesOptionId:
						result = System.Windows.MessageBoxResult.Yes;
						break;
					case Sidvall.Resources.ResourceKeys.NoOptionId:
						result = System.Windows.MessageBoxResult.No;
						break;
					default:
						break;
				}
			}
			System.Windows.MessageBox.Show(SystemContext.Current.MainWindow, message.Message, message.Title, buttons, icon, result);
			return Task.FromResult(true);
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			return "Dialog - MessageBox";
		}

		#endregion

		#endregion
		#region Constructors

		public MessageBoxDialog()
		{
		}

		#endregion
	}
}
